import Vue from "vue";
import Vuex from "vuex";
import axios from 'axios'
Vue.use(Vuex);

export const createStore = () => {
    return new Vuex.Store({
        state: () => ({
            posts: [],
        }),
        mutations: {
            setPosts(state, data) {
                state.posts = data;
            },
        },
        actions: {
            async getPosts({ commit }) {
                const { data } = await axios({
                    method: "GET",
                    url: "http://api.apishop.net/common/joke/getJokesByRandom",
                    params: {
                        apiKey: "NYQKSIp4d015f429fcb106f9a2d2fadf6a8b34ab28789b0",
                        pageSize: 20,
                    },
                });
                commit("setPosts", data.result);
            },
        },
    });
};
